import Router from 'koa-router'
import config from '../config/index.js'
import reply from '../wechat/reply'
import wechatMIddle from '../wechat-lib/middleware'

export const router = app => {
  const router = new Router()

  router.all('/wechat-hear', wechatMIddle(config.wechat, reply))

  // router.get('/wechat-hear', (ctx, next) => {
  //   // console.log(ctx.query)
  //   const { signature, nonce, timestamp, echostr } = ctx.query
  //   const token = config.wechat.token
  //   const str = [token, timestamp, nonce].sort().join('')
  //   const sha = sha1(str)
  //   require('../wechat')
  //
  //   if (sha === signature) {
  //     ctx.body = echostr
  //   } else {
  //     console.log(signature, sha)
  //     ctx.body = 'failed'
  //   }
  // })

  app.use(router.routes())
          .use(router.allowedMethods())
}
